﻿

namespace SGH
{
    class Stripe
    {
        public int[,,] stripe;//毛坯类型，长度，排数
        /// <summary>
        /// 余料条带
        /// </summary>
        public int[,,] OffcutStripe;
        public int[,] width;//毛坯类型，排数
        /// <summary>
        /// 余料宽度集合
        /// </summary>
        public int[,] OffcutWidth;
        int length;
        int typeNum;
        int rowNum;

        public Stripe()
        {
             length = Constant.sheetLength;
             typeNum = Constant.blankTypeNum;
             rowNum = Constant.blankRow;
             stripe=new int[typeNum,length+1,rowNum];
             width = new int[typeNum, rowNum];
             initStripe();
        }

        private void initStripe()
        {

            var min = Method.getMin(Constant.diameter);
            var row = (int)Constant.OffcutWidthLimit / min;
            OffcutWidth = new int[typeNum, row];
            OffcutStripe = new int[typeNum, length + 1, row];
            for (int i = 0; i < typeNum; i++)
            {
                //条带
                int w = Constant.diameter[i] + Constant.allowance[i];
                for (int l = w; l <= length; l++)
                {
                    int oddRow = (int)(l / w);
                    int evenRow = (int)((l - w / 2) / w);
                    for (int n = 0; n < rowNum; n++)
                    {
                        int blankNum = -1;
                        if (n % 2 == 0)
                        {
                             blankNum = n * (oddRow + evenRow) / 2 + oddRow;
                        }
                        else 
                        {
                             blankNum = (n+1) * (oddRow + evenRow) / 2;
                        }
                        stripe[i, l, n] = blankNum;
                    }
                    for (int n = 0; n < row; n++)
                    {
                        int blankNum = -1;
                        blankNum = (n + 1) * oddRow;
                        OffcutStripe[i, l, n] = blankNum;
                    }
                }
                //条带宽度
                for (int r = 0; r < rowNum; r++)
                {
                    width[i,r] =(int) (w + r * w * Constant.SQRT_THREE / 2+0.999);
                }

                //余料宽度
                for (int r = 0; r < row; r++)
                {
                    var offcutWidth = (int)(w + r * w + 0.999);
                    //if (offcutWidth> Constant.OffcutWidth) continue;
                    OffcutWidth[i,r] = offcutWidth;
                }
            }
        }

        

        public int getBlankNumByLengthandRow(int no,int l,int r)
        {
            return stripe[no, l, r-1];
        }
    }
}
